﻿using Microsoft.Win32;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WpfApp1.Tools;
using WpfApp1.ViewModel;

namespace WpfApp1
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {




        public CompareDataViewModel Compare1 { get; } = new ();
        public CompareDataViewModel Compare2 { get; } = new ();

        public ICommand CompareCMD { get; }
        public MainWindowViewModel()
        {
            Compare1.Title = "对比数据集合1";
            Compare2.Title = "对比数据集合2";

            CompareCMD = new RelayCommand(_ => { CompareCommand(); });

        }

        public void CompareCommand()
        {
            if(Compare1 == null || Compare2 == null)
            {
                MessageBox.Show("错误，实例为空！！！");
                return;
            }

            var data1 = Compare1._allData;
            var data2 = Compare2._allData;


            if (data1.Count == 0 || data2.Count == 0)
            {
                MessageBox.Show("数据为空，不用对比！！！");
                return;
            }

            CompareStringLists(data1, data2, out var onlyInData1, out var onlyInData2);



           var strOnlyInData1 = string.Join("\r\n", onlyInData1); 
           var strOnlyInData2 = string.Join("\r\n", onlyInData2);

            Compare1.DifferenceData= strOnlyInData1;
            Compare1.DifferenceDataCount = onlyInData1.Count;

            Compare2.DifferenceData= strOnlyInData2;
            Compare2.DifferenceDataCount = onlyInData2.Count;




        }

        public static void CompareStringLists(
            IEnumerable<string> list1,
            IEnumerable<string> list2,
            out List<string> onlyInList1,
            out List<string> onlyInList2)
        {
            var set1 = new HashSet<string>(list1);
            var set2 = new HashSet<string>(list2);

            onlyInList1 = set1.Except(set2).ToList();
            onlyInList2 = set2.Except(set1).ToList();
        }






        #region INotifyPropertyChanged接口实现
        /// <summary>
        /// 属性变更事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 触发属性变更通知
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

    }



}
